<script setup>
import {
  Edit,
  Delete
} from '@element-plus/icons-vue'
import { ref } from 'vue'

const categorys = ref([
  {
    "id": 1,
    "categoryName": "置业大厦",
    "categoryPrice": "4800",
    "createTime": "2024-07-31 00:50:45",
    "updateTime": "2024-07-31 01:04:00"
  },
  {
    "id": 2,
    "categoryName": "双创四人间",
    "categoryPrice": "3800",
    "createTime": "2024-07-31 14:20:10",
    "updateTime": "2024-07-31 14:20:10"
  }
])
import { dormitoryCategoryListService } from '@/api/Dormitory.js'
const dormitoryList = async () => {
  let result = await dormitoryCategoryListService();
  categorys.value = result.data;
}
dormitoryList();


//控制添加分类弹窗
const dialogVisible = ref(false)

//添加分类数据模型
const categoryModel = ref({
  categoryName: '',
  categoryPrice: ''
})
//添加分类表单校验
const rules = {
  categoryName: [
    { required: true, message: '请输入分类名称', trigger: 'blur' },
  ],
  categoryPrice: [
    { required: true, message: '请输入分类价格', trigger: 'blur' },
  ]
}

import { ElMessage } from 'element-plus'
//添加宿舍分类
import { addDormitoryCategoryService } from '@/api/Dormitory.js'
const addCategory = async () => {
  let result = await addDormitoryCategoryService(categoryModel.value)
  ElMessage.success(result.message ? result.message : '添加成功')

  dialogVisible.value = false;

  dormitoryList();
}

//控制标题展示
const title = ref('')

//展示编辑弹窗
const showDialog = (row) => {
  title.value = '编辑分类'
  dialogVisible.value = true
  categoryModel.value = row
}

//更新分类接口
import { updateDormitoryCategoryService } from '@/api/Dormitory.js'
const updateCategory = async () => {
  let result = await updateDormitoryCategoryService(categoryModel.value)
  ElMessage.success(result.message ? result.message : '修改成功')

  dialogVisible.value = false;

  dormitoryList();
}

//清楚添加分类数据模型
const clearData = () => {
  categoryModel.value = {
    categoryName: '',
    categoryPrice: ''
  }
}

//删除分类接口调用
import { deleteDormitoryCategoryService } from '@/api/Dormitory.js'
import { ElMessageBox } from 'element-plus'
const deleteCategory = (row) => {
  //提示用户，确认框
  ElMessageBox.confirm(
    '你确认要删除这条分类信息吗？',
    '温馨提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(async () => {
      //调用接口
      let result = await deleteDormitoryCategoryService(row.id)

      ElMessage({
        type: 'success',
        message: '删除成功',
      })
      //刷新列表
      dormitoryList()
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '删除取消',
      })
    })
}
// import {ExportCategoryData} from '@/api/Category.js'
// const exportData = async () => {
//   let result = await ExportCategoryData()
//   console.log(result)
// }


import axios from 'axios';

const exportData = async () => {
  try {
    const response = await axios({
      method: 'get',
      url: 'http://localhost:8002/category/exportData', // 替换为你的实际 API 端点
      responseType: 'blob' // 设置响应类型为 blob
    });

    // 创建一个隐藏的 <a> 元素来触发文件下载
    const link = document.createElement('a');
    link.href = URL.createObjectURL(response.data);
    link.download = '宿舍分类数据记录表.xlsx'; // 设置文件名
    link.click();


    // 释放 URL 对象
    URL.revokeObjectURL(link.href);

    // alert('数据导出成功');
  } catch (error) {
    if (error.response) {
      // 处理后端返回的错误信息
      alert('导出数据失败: ' + error.response.data);
    } else {
      alert('导出数据失败: ' + error.message);
    }
  }
};




</script>

<template>
  <div class="tools-div">
    <el-button type="success" size="small" @click="exportData">导出</el-button>
    <el-button type="primary" size="small">导入</el-button>
  </div>

  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>宿舍分类</span>
        <div class="extra">
          <el-button type="primary" @click="dialogVisible = true; title = '添加分类'; clearData()">添加分类</el-button>
        </div>
      </div>
    </template>
    <el-table :data="categorys" style="width: 100%">
      <el-table-column label="序号" width="100" type="index"> </el-table-column>
      <el-table-column label="分类名称" prop="categoryName"></el-table-column>
      <el-table-column label="分类价格" prop="categoryPrice"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
          <el-button :icon="Delete" circle plain type="danger" @click="deleteCategory(row)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
  </el-card>


  <!-- 添加分类弹窗 -->
  <el-dialog v-model="dialogVisible" :title="title" width="30%">
    <el-form :model="categoryModel" :rules="rules" label-width="100px" style="padding-right: 30px">
      <el-form-item label="分类名称" prop="categoryName">
        <el-input v-model="categoryModel.categoryName" minlength="1" maxlength="10"></el-input>
      </el-form-item>
      <el-form-item label="分类价格" prop="categoryPrice">
        <el-input v-model="categoryModel.categoryPrice" minlength="1" maxlength="15"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="title === '添加分类' ? addCategory() : updateCategory()"> 确认 </el-button>
      </span>
    </template>
  </el-dialog>

</template>

<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>
